package com.ruoyi.web.controller.message;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.message.domain.vo.MessageVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.message.domain.TMessage;
import com.ruoyi.message.service.ITMessageService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 消息推送Controller
 *
 * @author Bandit
 * @date 2022-11-01
 */
@RestController
@RequestMapping("/message/message")
public class TMessageController extends BaseController
{
    @Autowired
    private ITMessageService tMessageService;

    /**
     * 查询消息推送列表
     */

    @GetMapping("/list")
    public TableDataInfo list(TMessage tMessage)
    {
        startPage();
        List<TMessage> list = tMessageService.selectTMessageList(tMessage);
        return getDataTable(list);
    }

    @PostMapping("/read")
    public AjaxResult read(@RequestParam Long messageId) {
        Long userId = SecurityUtils.getUserId();
        return success(tMessageService.readMessage(userId, messageId));
    }

    /**
     * 导出消息推送列表
     */
    @PreAuthorize("@ss.hasPermi('message:message:export')")
    @Log(title = "消息推送", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TMessage tMessage)
    {
        List<TMessage> list = tMessageService.selectTMessageList(tMessage);
        ExcelUtil<TMessage> util = new ExcelUtil<TMessage>(TMessage.class);
        util.exportExcel(response, list, "消息推送数据");
    }

    /**
     * 获取消息推送详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tMessageService.selectTMessageById(id));
    }

    /**
     * 发送消息
     */
    @PreAuthorize("@ss.hasPermi('message:message:add')")
    @Log(title = "消息推送", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MessageVo messageVo)
    {
        return toAjax(tMessageService.insertMessageVo(messageVo));
    }

    /**
     * 修改消息推送
     */
    @PreAuthorize("@ss.hasPermi('message:message:edit')")
    @Log(title = "消息推送", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TMessage tMessage)
    {
        return toAjax(tMessageService.updateTMessage(tMessage));
    }

    /**
     * 删除消息推送
     */
    @PreAuthorize("@ss.hasPermi('message:message:remove')")
    @Log(title = "消息推送", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tMessageService.deleteTMessageByIds(ids));
    }
}
